﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package com.gaiaonline.monster.view.components.videoAd {
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import org.osflash.signals.*;
    import alecmce.ui.*;
    import com.gaiaonline.monster.view.components.app.panel.*;
    import flash.geom.*;
    import flash.text.*;
    import com.gaiaonline.monster.service.*;
    import com.tremormedia.acudeo.events.*;
    import com.tremormedia.acudeo.*;
    import com.tremormedia.acudeo.admanager.*;
    import com.tremormedia.acudeo.content.*;
    import com.tremormedia.acudeo.player.*;
    import com.tremormedia.acudeo.page.*;
    import flash.external.*;

    public class VideoAd extends Panel {

        private var asset:Asset;
        private var content:MovieClip;
        private var _adManager:IAdManager;
        private var flvPlayback;
        private var progressBar:MovieClip;
        private var progressLabel:TextField;
        private var muteButton:MovieClip;
        private var _complete:Signal;
        private var _videoAdPlaying:Boolean;
        private var isInMute:Boolean;
        private var progId:String;
        private var shopButton:SimpleButton;
        private var _openShop:Signal;

        public function VideoAd(_arg1:Asset, _arg2:String){
            this._complete = new Signal();
            this._openShop = new Signal();
            Security.allowDomain("http://objects.tremormedia.com/");
            this.progId = _arg2;
            this.asset = _arg1;
            addChild(_arg1.self);
            if (this.asset.isComplete){
                this.init();
            } else {
                this.asset.complete.add(this.init);
            };
            this._adManager = AdManager.getInstance();
            this._adManager.addEventListener(AcudeoEvent.PROGRAM_LOADED, this.onAdManagerProgramLoaded);
            this._adManager.addEventListener(AcudeoEvent.START_CONTENT, this.onAdComplete);
            this._adManager.addEventListener(AdEvent.VIDEOPROGRESS, this.onAdVideoProgress);
        }
        private function init(_arg1:Asset=null):void{
            this.content = MovieClip(_arg1.content);
            this.progressBar = MovieClip(this.content.progressBar.bar);
            this.flvPlayback = this.content.flvPlayback;
            this.progressLabel = this.content.timeLeft;
            this.shopButton = this.content.openShop;
            this.shopButton.addEventListener(MouseEvent.CLICK, this.onShopButtonClicked);
            this.muteButton = MovieClip(this.content.muteButton);
            this.muteButton.buttonMode = true;
            this.muteButton.addEventListener("click", this.onMuteClick);
            this.flvPlayback.addEventListener("ready", this.onFLVPlaybackReady);
        }
        private function onFLVPlaybackReady(_arg1:Object):void{
            var _local2:Player = new Player();
            _local2.videoRegion = new Rectangle(this.flvPlayback.x, this.flvPlayback.y, this.flvPlayback.width, this.flvPlayback.height);
            _local2.volume = 100;
            var _local3:Page = new Page();
            _local3.url = ExternalInterface.call("function(){return location.href}");
            this._adManager.init({player:_local2, page:_local3});
            this._adManager.setVolume((this.flvPlayback.volume * 100));
            addChild((this._adManager as Sprite));
            this._adManager.load({progId:this.progId});
        }
        private function onAdManagerProgramLoaded(_arg1:Object):void{
            this._adManager.openSlate();
            this.contentPreStart();
        }
        private function contentPreStart():void{
            var _local1:Content = new Content();
            _local1.id = "78262";
            _local1.url = this.flvPlayback.source;
            _local1.title = "TEST";
            this._adManager.setContent(_local1);
            this._videoAdPlaying = true;
            this._adManager.contentPreStart();
        }
        private function onMuteClick(_arg1:MouseEvent):void{
            if (this.isInMute){
                this._adManager.setVolume(50);
                this.muteButton.gotoAndStop("noMute");
                this.isInMute = false;
            } else {
                this._adManager.setVolume(0);
                this.muteButton.gotoAndStop("mute");
                this.isInMute = true;
            };
        }
        private function onAdComplete(_arg1:Object):void{
            this.removeListeners();
            this.removeChild(this.asset.self);
            this._complete.dispatch();
        }
        private function onAdVideoProgress(_arg1:AdEvent):void{
            this.progressBar.scaleX = (int(_arg1.ad.progress) / int(_arg1.ad.duration));
            this.progressLabel.text = ((Utils.formatTime(int(_arg1.ad.progress)) + " / ") + Utils.formatTime(int(_arg1.ad.duration)));
        }
        private function onShopButtonClicked(_arg1:MouseEvent):void{
            this._openShop.dispatch();
        }
        public function get complete():Signal{
            return (this._complete);
        }
        public function get openShop():Signal{
            return (this._openShop);
        }
        private function onFLVPlaybackComplete(_arg1:Object):void{
            this._adManager.contentEnd();
        }
        private function removeListeners():void{
            this._adManager.removeEventListener(AcudeoEvent.PROGRAM_LOADED, this.onAdManagerProgramLoaded);
            this._adManager.removeEventListener(AcudeoEvent.START_CONTENT, this.onAdComplete);
            this._adManager.removeEventListener(AdEvent.VIDEOPROGRESS, this.onAdVideoProgress);
            this.shopButton.removeEventListener(MouseEvent.CLICK, this.onShopButtonClicked);
            this.flvPlayback.removeEventListener("ready", this.onFLVPlaybackReady);
        }

    }
}//package com.gaiaonline.monster.view.components.videoAd 
